package utils;

import java.awt.Point;
//import java.io.BufferedReader;
//import java.io.BufferedWriter;
//import java.io.File;
//import java.io.FileNotFoundException;
//import java.io.FileReader;
//import java.io.FileWriter;
//import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
//import java.util.StringTokenizer;

import dao.DbClass;

import entity.Player;
import entity.Pokemon;

public class SaveManager {
	DbClass db = new DbClass();
	public void saveGameDB(Player player) throws SQLException {
		ResultSet rs = null;

		rs = db.executeQuery("SELECT * FROM PLAYER");
		if (rs.next()) {

			db.execute("UPDATE player set xposition=" + player.getX()
					+ ", yposition=" + player.getY() + ", xlocation="
					+ player.getXLocation() + ", ylocation="
					+ player.getYLocation());
			for (int i = 0; i < player.getPokemonList().size(); i++) {
				rs = db.executeQuery("SELECT * FROM player_pokemons where id ="+player.getPokemonList().get(i).getId_banco());
				if(rs.next()){
					db.execute("UPDATE player_pokemons set activated="
							+ player.getPokemonList().get(i).getActivated()
							+ ", level="
							+ player.getPokemonList().get(i).getLevel()
							+ ", experience="
							+ player.getPokemonList().get(i).getExperience()
							+ ", currentHp="
							+player.getPokemonList().get(i).getCurrentHp()
							+ " WHERE id="+player.getPokemonList().get(i).getId_banco());
				}else{
					db.execute("INSERT INTO player_pokemons (id_pokemon, level, exeperience, activated, currentHp)values("+
							player.getPokemonList().get(i).getId() + "," +
							player.getPokemonList().get(i).getLevel()+ "," +
							player.getPokemonList().get(i).getExperience()+ "," +
							player.getPokemonList().get(i).getActivated()+ ","+
							player.getPokemonList().get(i).getCurrentHp()+ ","+
							")");
				}				
			}
		}else{
			Player player1 = new Player();
			db.execute("INSERT INTO PLAYER VALUES("+player1.getX()+
					","+player1.getY()+
					","+player1.getYLocation()+
					","+player1.getXLocation()+")");
		}
	}

	public Player openGameDB() throws SQLException{
		ResultSet rs = null;
		Player player = new Player(new Point(5,5), new Point(7, 9));
		rs = db.executeQuery("SELECT * FROM PLAYER");
		if (!rs.next()) {
			saveGameDB(null);
			openGameDB();
		}else{
			ArrayList<Pokemon> pokemonList = new ArrayList<Pokemon>();
			player.setPosition(new Point(rs.getInt("xposition"), rs.getInt("yposition")));
			player.setLocation(new Point(rs.getInt("xlocation"), rs.getInt("ylocation")));
			rs = db.executeQuery("SELECT * FROM player_pokemons");
			while(rs.next()){
				ResultSet rs2 = db.executeQuery("SELECT * FROM pokemons where ID="+rs.getInt("id_pokemon"));
				Pokemon pok = new Pokemon(rs2.getInt("id"), rs2.getString("name"), rs2.getInt("hp"), rs2.getInt("attack"),
						rs2.getInt("defence"),rs2.getInt("speed"), rs2.getInt("expGain"),rs2.getString("type1"),
						rs2.getString("type2"),rs2.getString("abilitie1"),rs2.getString("abilitie2"),rs2.getInt("rarity"));
				pok.setId_banco(rs.getInt("id"));
				pok.setLevel(rs.getInt("level"));
				pok.setExperience(rs.getInt("experience"));
				pok.setCurrentHp(rs.getInt("currentHp"));
				pokemonList.add(pok);
			}
			player.setPokemonList(pokemonList);			
		}
		return player;
	}
	
	public void resetGameDB(){
		db.execute("DELETE FROM player");
		db.execute("DELETE FROM player_pokemons");
	}
	
	public Pokemon getPokemonAtributes(int id) throws SQLException{
		ResultSet rs = null;
		rs = db.executeQuery("SELECT * FROM pokemons where id="+id);
		Pokemon pok = new Pokemon(rs.getInt("id"), rs.getString("name"), rs.getInt("hp"), rs.getInt("attack"),
				rs.getInt("defence"),rs.getInt("speed"), rs.getInt("expGain"),rs.getString("type1"),
				rs.getString("type2"),rs.getString("abilitie1"),rs.getString("abilitie2"),rs.getInt("rarity"));
		return pok;
	}
//	 public static void saveGame(Player player) {
//	 BufferedWriter wr = null;
//	 String save = new String();
//	 if(player.getPokemon().getCurrentHp() == 1000){
//	 player.getPokemon().setCurrentHp(ResourceManager.getPokemon(player.getPokemon().getId()).getHp());
//	 System.out.println("Setando hp");
//	 }
//	 save = player.getX() + "," + player.getY() + ","
//	 + player.getXLocation() + "," + player.getYLocation() + "," +
//	 player.getPokemon().getId() +","+
//	 player.getPokemon().getLevel() +","+player.getPokemon().getExperience() +
//	 "," + player.getPokemon().getCurrentHp();
//	 try {
//	 wr = new BufferedWriter(new FileWriter("save.txt"));
//	 wr.write(save);
//	 wr.close();
//	 } catch (IOException e) {
//	 e.printStackTrace();
//	 }
//	
//	 }
//	
//	 public static Player openGame(Player player) {
//	 player = new Player();
//	 File save = new File("save.txt");
//	 if (save.exists()) {
//	 FileReader reader = null;
//	 try {
//	 reader = new FileReader(save);
//	 } catch (FileNotFoundException e) {
//	 System.out.println("Impossivel achar o arquivo");
//	 }
//	 BufferedReader rd = new BufferedReader(reader);
//	 StringTokenizer st;
//	 String line = null;
//	 try {
//	 line = rd.readLine();
//	 } catch (IOException e) {
//	 }
//	 st = new StringTokenizer(line, ",");
//	 player.setPosition(new Point(Integer.parseInt(st.nextToken()),
//	 Integer.parseInt(st.nextToken())));
//	 player.setLocation(new Point(Integer.parseInt(st.nextToken()),
//	 Integer.parseInt(st.nextToken())));
//	 int pokemonId = Integer.parseInt(st.nextToken());
//	 int pokemonLevel = Integer.parseInt(st.nextToken());
//	 int pokemonExp = Integer.parseInt(st.nextToken());
//	 int pokemonHp = Integer.parseInt(st.nextToken());
//	
//	 player.setPokemon(ResourceManager.getPokemon(pokemonId));
//	 player.getPokemon().setLevel(pokemonLevel);
//	 player.getPokemon().setExperience(pokemonExp);
//	 player.getPokemon().setCurrentHp(pokemonHp);
//	 try {
//	 rd.close();
//	 reader.close();
//	 } catch (IOException e) {
//	 System.out.println("Impossivel fechar o fluxo");
//	 }
//	
//	 return player;
//	 } else {
//	 player.setLocation(new Point(5, 5));
//	 player.setPosition(new Point(7, 9));
//	 player.setPokemon(new Pokemon(0));
//	 saveGame(player);
//	 return player;
//	 }
//	 }
//	
//	 public static void resetGame() {
//	 File save = new File("save.txt");
//	 System.gc();//Garbage Collector :)
//	 if (save.exists()) {
//	 save.delete();
//	 System.out.println("Game Resetado");
//	 }
//	 }
}
